
set more off
************************************
*Mixed Logit Models 			   *
*(Only Parameters Random with z>1) *
************************************
	local group = e(group)
	local dep=e(depvar)
	indeplist, local
	local X = r(X)	
	qui predict cp_`m', p
	qui egen total = count(1), by(`dep')
	qui foreach x of varlist `X'{
		qui bysort `group': egen SQ_`x' = sum(`x' * cp)
		gen ZQ_`x'= 0.5*(`x' - SQ_`x')^2
		egen group=group(`x')
		qui sum group
		local mg=r(max)
		drop group
		if `mg'<6 {
			egen tt= count(1), by(`dep' `x')
			gen tt2=tt/total
			qui sum tt2
			local mt=r(min)
			if  `mt'<0.01 local sep `sep' `x'
			drop tt2 tt
			}
		}
	drop total
	clogit `dep' `X' ZQ_* if majsit==0,group(`group') 
	drop SQ_* ZQ_* cp_
	
		preserve 
			parmest, norestore
			qui keep if substr(parm,1,3)=="ZQ_"
			qui replace parm=substr(parm,4,.)	
			qui keep eq z parm 
			qui reshape wide z, i(eq) j(parm) s
			drop eq
			renvars, map(substr("@", 2, .))
			qui foreach var of varlist _all{
				qui replace `var'=abs(`var')
				qui sum `var'
					if r(mean)<=1 local fix `fix' `var' 
					if r(mean)>1 & r(mean)<=1.96 local rand `rand' `var' 
					if r(mean)>1.96 local sig `sig' `var'
					}
		di as text "Significant Random Variables: " as text "`sig'"
		di as text "Random Variables (z>1): " as text "`rand'"
		di as text "Fixed Variables: " as text "`fix'"
		di as text "Potential Seperation Problem: " as text "`sep'"	
		restore
	mixlogit `dep' `fix' if majsit==0, group(`group') rand(`rand' `sig') nrep(100)
	macro drop _fix _rand _sig _sep





